/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wrapp.floatlabelededittext;

import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;

/**
 * 获取自定义属性类
 *
 * @since 2021-08-04
 */
public class TypedArray {
    private AttrSet attrSet;

    /**
     * 构造方法
     *
     * @param attrSet
     */
    public TypedArray(AttrSet attrSet) {
        this.attrSet = attrSet;
    }

    /**
     * 获取vp fp属性
     *
     * @param value
     * @param defaultValue
     * @return int
     */
    public int getDimensionValue(String value, int defaultValue) {
        if (attrSet.getAttr(value).isPresent()) {
            return attrSet.getAttr(value).get().getDimensionValue();
        } else {
            return defaultValue;
        }
    }

    /**
     * 获取颜色属性
     *
     * @param value
     * @param defaultValue
     * @return Color
     */
    public Color getColor(String value, int defaultValue) {
        if (attrSet.getAttr(value).isPresent()) {
            return attrSet.getAttr(value).get().getColorValue();
        } else {
            return new Color(defaultValue);
        }
    }

    /**
     * 获取float属性
     *
     * @param value
     * @param defaultValue
     * @return float
     */
    public float getFloat(String value, float defaultValue) {
        if (attrSet.getAttr(value).isPresent()) {
            return attrSet.getAttr(value).get().getFloatValue();
        } else {
            return defaultValue;
        }
    }

    /**
     * 获取int属性
     *
     * @param value
     * @param defaultValue
     * @return int
     */
    public int getInt(String value, int defaultValue) {
        if (attrSet.getAttr(value).isPresent()) {
            return attrSet.getAttr(value).get().getIntegerValue();
        } else {
            return defaultValue;
        }
    }

    /**
     * 获取Boolean属性
     *
     * @param value
     * @param defaultValue
     * @return boolean
     */
    public boolean getBoolean(String value, boolean defaultValue) {
        if (attrSet.getAttr(value).isPresent()) {
            return attrSet.getAttr(value).get().getBoolValue();
        } else {
            return defaultValue;
        }
    }

    /**
     * 获取String属性
     *
     * @param value
     * @return String
     */
    public String getString(String value) {
        if (attrSet.getAttr(value).isPresent()) {
            return attrSet.getAttr(value).get().getStringValue();
        } else {
            return null;
        }
    }

    /**
     * 判断是否声明当前属性
     *
     * @param value
     * @return boolean
     */
    public boolean hasValue(String value) {
        if (attrSet.getAttr(value).isPresent()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取Element值
     *
     * @param value
     * @return Element
     */
    public Element getElement(String value) {
        if (attrSet.getAttr(value).isPresent()) {
            return attrSet.getAttr(value).get().getElement();
        } else {
            return null;
        }
    }
}
